package com.sixmac.service.impl;

import com.sixmac.controller.common.CommonController;
import com.sixmac.core.Constant;
import com.sixmac.dao.RecordDao;
import com.sixmac.entity.Record;
import com.sixmac.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2016/5/19 0019 上午 10:13.
 */
@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    private RecordDao recordDao;

    @Override
    public List<Record> findAll() {
        return recordDao.findAll();
    }

    @Override
    public Page<Record> find(int pageNum, int pageSize) {
        return recordDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<Record> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public Record getById(int id) {
        return recordDao.findOne(id);
    }

    @Override
    public Record deleteById(int id) {
        Record record = getById(id);
        recordDao.delete(record);
        return record;
    }

    @Override
    public Record create(Record record) {
        return recordDao.save(record);
    }

    @Override
    public Record update(Record record) {
        return recordDao.save(record);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public List<Record> findListNew() {
        return recordDao.findListNew(CommonController.getOldDate());
    }
}